package com.lanou.moving.adapter;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.moving.R;
import com.lanou.moving.entity.RadioRoot;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/24.
 */
public class RadioStationAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<RadioRoot.DatasEntity.DataEntity> dataEntityArrayList;
    private MediaPlayer mediaPlayer;
    private List<CheckBox> checkBoxes= new ArrayList<>();

    public RadioStationAdapter(Context context, ArrayList<RadioRoot.DatasEntity.DataEntity> dataEntityArrayList
            , MediaPlayer mediaPlayer) {
        this.context = context;
        this.dataEntityArrayList = dataEntityArrayList;
        this.mediaPlayer = mediaPlayer;

    }

    @Override
    public int getCount() {
        return dataEntityArrayList == null ? 0 : dataEntityArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataEntityArrayList == null ? null : dataEntityArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {

            convertView = LayoutInflater.from(context).inflate(R.layout.radiao_listview_item, null);
            holder = new ViewHolder();
            holder.radio_listview_img = (ImageView) convertView.findViewById(R.id.radio_listview_img);
            holder.radio_tag_name = (TextView) convertView.findViewById(R.id.radio_tag_name);
            holder.radio_listen_count = (TextView) convertView.findViewById(R.id.radio_listen_count);
            holder.radio_play_img = (CheckBox) convertView.findViewById(R.id.radio_play_img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.radio_tag_name.setText(dataEntityArrayList.get(position % dataEntityArrayList.size()).getTag_name());
        String radio_listen_count = String.valueOf(dataEntityArrayList.get(position % dataEntityArrayList.size()).getListen_count());
        holder.radio_listen_count.setText(radio_listen_count);
        Picasso.with(context).load(dataEntityArrayList.get(position % dataEntityArrayList.size()).getLarge_pic_url()).into(holder.radio_listview_img);
        if (mediaPlayer.isPlaying()){
            holder.radio_play_img.setBackgroundResource(R.mipmap.mv_btn_pause_prs);
        }else {
            holder.radio_play_img.setBackgroundResource(R.mipmap.mv_btn_play_prs);
        }
        checkBoxes.add(holder.radio_play_img);
        Log.i("Sysout","checkBoxes"+checkBoxes.size());
        return convertView;
    }

    //根据position改变相应的CheckBox状态
    public void setChecked(int position){
        checkBoxes.get(position).setChecked(true);
        Log.i("Sysout", "check->in");

    }

    public static class ViewHolder {
        private ImageView radio_listview_img;
        private TextView radio_tag_name, radio_listen_count;
        public CheckBox radio_play_img;

    }


}
